﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NJL.WebUsers
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 网站编号
        /// </summary>
        public string WEBSITE_CODE
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["WebSiteCode"]; }
        }

        /// <summary>
        /// 网站名称
        /// </summary>
        public string WEBSITE_NAME
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["WebSiteName"]; }
        }

        /// <summary>
        /// 网站域名
        /// </summary>
        public string WEBSITE_DOMAIN
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["WebSiteDomain"]; }
        }

        /// <summary>
        /// 网站图片域名
        /// </summary>
        public string WEBSITE_IMAGE_DOMAIN
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["WebSiteImageDomain"]; }
        }

        /// <summary>
        /// 网站默认城市编号
        /// </summary>
        public string WEBSITE_DEFAULT_CITY_CODE
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["DefaultCityCode"]; }
        }

        /// <summary>
        /// 网站默认城市名称
        /// </summary>
        public string WEBSITE_DEFAULT_CITY_NAME
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["DefaultCityName"]; }
        }

        /// <summary>
        /// 当前网站城市编号
        /// </summary>
        public string WEBSITE_FOCUS_CITY_CODE
        {
            get
            {
                if (Request.QueryString["cc"] != null)
                {
                    Model.NJL_CS_AREA model = new BLL.NJL_CS_AREA().GetModel(WEBSITE_CODE, Request.QueryString["cc"].ToLower());
                    if (null != model)
                    {
                        return model.AREA_CODE;
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 当前网站城市名称
        /// </summary>
        public string WEBSITE_FOCUS_CITY_NAME
        {
            get
            {
                if (WEBSITE_FOCUS_CITY_CODE != "")
                {
                    return new BLL.NJL_CS_AREA().GetModel(WEBSITE_FOCUS_CITY_CODE).AREA_NAME;
                }
                else
                {
                    return "全省";
                }
            }
        }

        /// <summary>
        /// 当前网站城市名称拼音
        /// </summary>
        public string WEBSITE_FOCUS_CITY_NAME_PINYIN
        {
            get
            {
                if (WEBSITE_FOCUS_CITY_CODE != "")
                {
                    return new BLL.NJL_CS_AREA().GetModel(WEBSITE_FOCUS_CITY_CODE).AREA_NAME_PINYIN;
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 获取当前网站的基本信息
        /// </summary>
        public Model.NJL_SYS_BASE WEBSITE_SYS_BASE
        {
            get { return new BLL.NJL_SYS_BASE().GetModelByCache(WEBSITE_CODE); }
        }

        public BasePage()
        {
            this.Load += new EventHandler(CheckDomain);
            this.Load += new EventHandler(Basepage_Load);
        }
        void Basepage_Load(Object sender, EventArgs e)
        {
            if (Session["userid"] == null || Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
        }

        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserId
        {
            get
            {
                if (Session["userid"] != null)
                {
                    return Session["userid"].ToString();
                }
                else
                {
                    return "";
                }
            }

        }
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                if (Session["username"] != null)
                {
                    return Session["username"].ToString();
                }
                else
                {
                    return "";
                }
            }

        }
        /// <summary>
        /// 网站编号
        /// </summary>
        public string WebSiteCode
        {
            get
            {
                return WEBSITE_CODE;
            }
        }

        /// <summary>
        /// 用户类型
        /// </summary>
        public string UserType
        {
            get
            {
                if (Session["usertype"] != null)
                {
                    return Session["usertype"].ToString();
                }
                else
                {
                    return "";
                }
            }
        }

        #region 后台图片上传、读取、删除公共处理方法

        public string SaveUploadFile(HttpFileCollection ContractFile)
        {
            string strImageID = "";
            string strFileName = FileManageGetFileName();
            string strFileType = FileManageGetFileType(ContractFile);
            string strFileSavePath = FileManageGetFileUrlPath(WebSiteCode, UserType);
            string strFileLocalPath = HttpContext.Current.Server.MapPath(strFileSavePath);
            //
            if (Royalsoft.Common.FileManage.SaveFile(ContractFile, strFileLocalPath, strFileName, strFileType))
            {
                BLL.NJL_IMAGE bll = new BLL.NJL_IMAGE();
                Model.NJL_IMAGE model = new Model.NJL_IMAGE();

                model.IMAGE_NAME = strFileName;
                model.IMAGE_TYPE = strFileType;
                model.IMAGE_URL_PATH = strFileSavePath;
                model.IMAGE_LOCAL_PATH = strFileLocalPath;
                model.IMAGE_FROM_TYPE = Convert.ToInt32(UserType);
                model.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(111, 999).ToString();
                model.CREATE_USER_ID = UserId;
                model.CREATE_USER_NAME = UserName;
                model.CREATE_DATE_TIME = System.DateTime.Now;

                bll.Add(model);

                strImageID = model.ID;
            }

            return strImageID;
            //
        }
        public string SaveUploadFile(HttpFileCollection ContractFile, int fileUploadIndex)
        {
            string strImageID = "";
            string strFileName = FileManageGetFileName();
            string strFileType = FileManageGetFileType(ContractFile, fileUploadIndex);
            string strFileSavePath = FileManageGetFileUrlPath(WebSiteCode, UserType);
            string strFileLocalPath = HttpContext.Current.Server.MapPath(strFileSavePath);
            //
            if (Royalsoft.Common.FileManage.SaveFile(ContractFile, strFileLocalPath, strFileName, strFileType, fileUploadIndex))
            {
                BLL.NJL_IMAGE bll = new BLL.NJL_IMAGE();
                Model.NJL_IMAGE model = new Model.NJL_IMAGE();
                model.IMAGE_NAME = strFileName;
                model.IMAGE_TYPE = strFileType;               
                model.IMAGE_URL_PATH = strFileSavePath;
                model.IMAGE_LOCAL_PATH = strFileLocalPath;
                model.IMAGE_FROM_TYPE = Convert.ToInt32(UserType);
                model.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(111, 999).ToString();
                model.CREATE_USER_ID = UserId;
                model.CREATE_USER_NAME = UserName;
                model.CREATE_DATE_TIME = System.DateTime.Now;
                bll.Add(model);
                strImageID = model.ID;
            }

            return strImageID;
            //
        }
        /// <summary>
        /// 获得文件存储路径
        /// </summary>
        /// <param name="WebsiteCode">网站编号</param>
        /// <param name="UserType">用户类型：1：User，2：Admin</param>
        /// <returns></returns>
        public string FileManageGetFileUrlPath(string WebsiteCode, string UserType)
        {
            UserType = "1";
            string strFileSavePath = "";
            DateTime dtime = DateTime.Now;

            strFileSavePath = System.Configuration.ConfigurationManager.AppSettings["UploadFileSavePath"] + "/" + WebsiteCode + "/" + UserType + "/" + dtime.ToString("yyyy") + "/" + dtime.ToString("MM") + "/" + dtime.ToString("dd") + "/";

            return strFileSavePath;
        }

        /// <summary>
        /// 获得文件物理路径
        /// </summary>
        /// <param name="FileUrlPath">文件URL路径</param>
        /// <returns></returns>
        public string FileManageGetFileLocalPath(string FileUrlPath)
        {
            string strFileSavePath = "";
            DateTime dtime = DateTime.Now;

            strFileSavePath = System.Configuration.ConfigurationManager.AppSettings["UploadFileSavePath"] + "/" + WebSiteCode + "/" + dtime.ToString("yyyy") + "/" + dtime.ToString("MM") + "/" + dtime.ToString("dd") + "/";

            return strFileSavePath;
        }

        /// <summary>
        /// 获得新文件名称
        /// </summary>
        /// <returns></returns>
        public string FileManageGetFileName()
        {
            return System.Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 获得文件的类型
        /// </summary>
        /// <param name="ContractFile"></param>
        /// <returns></returns>
        public string FileManageGetFileType(HttpFileCollection ContractFile)
        {
            string strFileType = "";
            if (ContractFile[0].ContentLength > 0)
            {
                strFileType = ContractFile[0].FileName.Substring(ContractFile[0].FileName.LastIndexOf("."));
            }
            return strFileType;
        }

        /// <summary>
        /// 获得文件的类型
        /// </summary>
        /// <param name="ContractFile"></param>
        /// <returns></returns>
        public string FileManageGetFileType(HttpFileCollection ContractFile, int fileUploadIndex)
        {
            string strFileType = "";
            if (ContractFile[fileUploadIndex].ContentLength > 0)
            {
                strFileType = ContractFile[fileUploadIndex].FileName.Substring(ContractFile[fileUploadIndex].FileName.LastIndexOf("."));
            }
            return strFileType;
        }
        /// <summary>
        /// 通过图片编号获取图片URL路径
        /// </summary>
        /// <param name="ImageCode"></param>
        /// <returns></returns>
        public static string GetImageUrl(string ImageCode)
        {
            BLL.NJL_IMAGE bll = new BLL.NJL_IMAGE();
            Model.NJL_IMAGE model = bll.GetModel(ImageCode);

            if (model != null)
            {
                return System.Configuration.ConfigurationManager.AppSettings["WebSiteImageDomain"] + model.IMAGE_URL_PATH + model.IMAGE_NAME + model.IMAGE_TYPE;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 通过图片编号获取图片URL路径
        /// </summary>
        /// <param name="ImageCode"></param>
        /// <returns></returns>
        public static string GetImageUrl(string ImageCode, string SizeType)
        {
            BLL.NJL_IMAGE bll = new BLL.NJL_IMAGE();

            Model.NJL_IMAGE model = bll.GetModel(ImageCode);

            if (model != null)
            {
                return System.Configuration.ConfigurationManager.AppSettings["WebSiteImageDomain"] + "/?st=" + SizeType.Trim() + "&id=" + model.ID;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 通过图片编号删除图片
        /// </summary>
        /// <param name="ImageCode"></param>
        /// <returns></returns>
        public bool DeleteImageByCode(string ImageCode)
        {
            BLL.NJL_IMAGE bll = new BLL.NJL_IMAGE();

            Model.NJL_IMAGE model = bll.GetModel(ImageCode);
            if (model != null)
            {
                if (Royalsoft.Common.FileManage.DeletePicture(model.IMAGE_URL_PATH + model.IMAGE_NAME + model.IMAGE_TYPE))
                {
                    return bll.Delete(ImageCode);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region SQL注入过滤
        /// <summary>
        ///SQL注入过滤
        /// </summary>
        /// <param name="InText">要过滤的字符串</param>
        /// <returns>如果参数存在不安全字符，则返回true</returns>
        public static bool SqlFilter(string InText)
        {
            string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
            if (InText == null)
                return false;
            foreach (string i in word.Split('|'))
            {
                if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 提交内容屏蔽过滤词
        public static bool CheckBlackWord(string InText)
        {
            IList<NJL.Model.NJL_SYS_BLACK_WORD> list = new BLL.NJL_SYS_BLACK_WORD().GetModelListByCache();
            if (InText == null)
                return false;
            foreach (NJL.Model.NJL_SYS_BLACK_WORD i in list)
            {
                if ((InText.ToLower().IndexOf(i.BLACK_WORD.ToString()) > -1))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
        #region 积分规则
        /// <summary>
        /// 积分处理
        /// </summary>
        /// <param name="TypeCode">用户ID</param>
        /// <param name="TypeCode">积分编号</param>
        public bool HandleIntegral(string User_ID, string TypeCode, out string message)
        {
            message = "";
            BLL.NJL_CS_INTEGRAL_TYPE bllCS = new BLL.NJL_CS_INTEGRAL_TYPE();
            Model.NJL_CS_INTEGRAL_TYPE modelCS = bllCS.GetModelByCache(TypeCode);
            BLL.NJL_USER bllUser = new BLL.NJL_USER();
            Model.NJL_USER modelUser = bllUser.GetModel(User_ID);
            BLL.NJL_REG_INTEGRAL bllReg = new BLL.NJL_REG_INTEGRAL();

            if (modelCS != null && modelUser != null)
            {
                Model.NJL_REG_INTEGRAL modelReg = new Model.NJL_REG_INTEGRAL();
                modelReg.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                modelReg.NJL_SYS_BASE_ID = WEBSITE_CODE;
                modelReg.NJL_USER_ID = modelUser.ID;
                modelReg.TYPE_CODE = modelCS.TYPE_CODE;
                modelReg.INTEGRAL = modelCS.INTEGRAL;
                modelReg.CREATE_DATE_TIME = System.DateTime.Now;
                bllReg.Add(modelReg);
                message = modelCS.TYPE_NAME;
                modelUser.INTEGRAL = modelUser.INTEGRAL + modelCS.INTEGRAL;
                return bllUser.Update(modelUser);
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 301处理
        //301处理
        private void CheckDomain(Object sender, EventArgs e)
        {
            string strDomain = Request.Url.Host;
            if (strDomain == "localhost")
                return;
            if (strDomain != WEBSITE_DOMAIN)
            {
                string strURL = Request.Url.ToString();
                strURL = strURL.Substring(7);
                strURL = strURL.Substring(strURL.IndexOf("/"));
                strURL = "http://" + WEBSITE_DOMAIN + strURL;
                //strURL = "http://" + WEBSITE_DOMAIN;

                Response.Redirect(strURL, true);
                //Response.Redirect(Request.Url.ToString().Replace("51maihua.cn", "www.51maihua.cn"), true);
            }
        }
        #endregion

        #region 页面调用短信发送接口
        public int SendSMS(string Mobile, string SMSContent)
        {
            int i = Royalsoft.Common.SMS.SendSMS(System.Configuration.ConfigurationManager.AppSettings["SMSSN"], Mobile, SMSContent, "5");

            return i;

            //if (1 == i)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }
        #endregion
    }

}